package cn.cs.yangtai.service.impl;

import cn.cs.yangtai.entity.Comminicate;
import cn.cs.yangtai.entity.Expert;
import cn.cs.yangtai.entity.Score;
import cn.cs.yangtai.entity.vo.PageResult;
import cn.cs.yangtai.mapper.ExpertMapper;
import cn.cs.yangtai.mapper.ScoreMapper;
import cn.cs.yangtai.service.ScoreService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;

/**
 * (Score)表服务实现类
 *
 * @author makejava
 * @since 2024-12-17 13:12:11
 */
@Service("scoreService")
public class ScoreServiceImpl implements ScoreService {
    @Resource
    private ScoreMapper scoreMapper;
    @Resource
    private ExpertMapper expertMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param scoreId 主键
     * @return 实例对象
     */
    @Override
    public Score queryById(Integer scoreId) {
        return this.scoreMapper.queryById(scoreId);
    }

    /**
     * 分页查询
     *
     * @param cuttentPage 本页
     * @param pageSize    分页大小
     * @return 查询结果
     */
    @Override
    public PageResult<Score> queryByPage(Integer cuttentPage, Integer pageSize) {
        PageHelper.startPage(cuttentPage, pageSize);
        Page<Score> scores = scoreMapper.queryAllByLimit(new Page<>());
        return PageResult.restPage(scores);
    }

    /**
     * 新增数据
     *
     * @param score 实例对象
     * @return 实例对象
     */
    @Override
    public Score insert(Score score) {
        this.scoreMapper.insert(score);
        return score;
    }

    /**
     * 修改数据
     *
     * @param score 实例对象
     * @return 实例对象
     */
    @Override
    public Score update(Score score) {
        this.scoreMapper.update(score);
        return this.queryById(score.getScoreId());
    }

    /**
     * 通过主键删除数据
     *
     * @param scoreId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer scoreId) {
        return this.scoreMapper.deleteById(scoreId) > 0;
    }

    /**
     * 添加评分
     *
     * @param score
     * @return
     */
    @Override
    public Score addScore(Score score) {
        try {
            int insert = scoreMapper.insert(score);
            if(insert != 0){
            List<Score> score1 = scoreMapper.queryByIdM(score.getScoreEXpertId());
            float sum = 0;
            float avg = 0;
            for (Score score2 : score1
            ) {
                sum += score2.getScore();
            }
            avg = sum / score1.size();
            //四舍五入,保留小数后两位
            avg = Math.round(avg * 100.0f) / 100.0f;
            //将最终评分写入专家表
            Expert expert = expertMapper.queryById(score.getScoreEXpertId());
            expert.setEScore(avg);
            expertMapper.update(expert);
            return score;
            }else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("评分插入过程异常!!!", e);
        }
    }
}
